import CONFIG from '@/common/config.js'
import useDataStore from "@/common/store/data.js"


export const $cdn_img = (path) => {
	// #ifdef H5
	if (path.startsWith('/static/')) return  CONFIG.$file_path  + path
	// #endif
	
	// #ifndef H5
	if (path.startsWith('/static/')) return path
	// #endif
	
	if (path.startsWith('http')) return path

	return CONFIG.$ossurl + path
}
export const $clearAppStore = function() {
	uni.setStorageSync("token", '')
	uni.setStorageSync("userInfo", '')
	useDataStore().userInfo = {}
	useDataStore().is_lawyer = false
	console.log('清除缓存::::页面缓存')
}


export const $sys = function() {
	return uni.getSystemInfoSync()
}

export const $getStatusBarHeight = () => {
	// #ifndef MP
	return sys().statusBarHeight + 'px'
	// #endif
	// #ifdef MP
	return sys().statusBarHeight + 4 + 'px'
	// #endif

}
export const $callPhone = async (phoneNumber) => {
	phoneNumber && uni.makePhoneCall({
		phoneNumber: phoneNumber,
		success(res) {}
	});
}
export const $outLogin = (path) => {
	$clearAppStore()
	uni.$u.route({
		auth:false,
		type: 'switchTab',
		url: '/pages/tabbar/mine'
	})
}

//导航栏高度  使用u-navbar 组件使用此方法计算导航栏高度
export const $NavBarHeight = function(num = 0) {
	return uni.$u.addUnit(uni.$u.getPx(uni.$u.props.navbar.height) + uni.$u.sys().statusBarHeight + num, 'px')
}
//导航栏信息  不使用u-navbar 组件使用此方法计算导航栏高度
export const $navStyle = function(current, status) {
	let {
		windowWidth,
		platform,
		statusBarHeight
	} = uni.getSystemInfoSync(), navinfo = {
		paddingRight: 16
	}, menuButton = {};
	//小程序胶囊信息
	// #ifdef MP
	menuButton = uni.getMenuButtonBoundingClientRect()
	navinfo.menuButtonHeight = menuButton.height
	navinfo.paddingRight = windowWidth - menuButton.right + menuButton.width
	// #endif

	// #ifdef APP-PLUS
	menuButton.top = statusBarHeight || 30
	// #endif 

	// #ifdef H5
	menuButton.top = 12
	// #endif 

	navinfo.height = menuButton.top + 8 + menuButton.height //头部高度
	navinfo.paddingTop = menuButton.top //小程序胶囊距离顶部高度

	// #ifdef APP-PLUS||H5
	navinfo.height = menuButton.top + 32
	// #endif

	navinfo.platform = platform

	return navinfo
}

export const $uploadImg = (filePath, data) => {
	return uni.$u.http.upload('/common/upload', {
		filePath, // 要上传文件资源的路径。
		name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
		header: {
			Authorization: uni.getStorageSync('userInfo').token,
		},
		formData: {
			...data
		}, // HTTP 请求中其他额外的 form data
		getTask: (task, options) => {
			console.log(task, 'task')
		},
	})
}
export const $isLogin = function() {
	return new Promise((resolve, reject) => {
		if (uni.getStorageSync("token")) {
			resolve()
		} else {
			uni.navigateTo({
				url: '/pages/login'
			})
			reject()
		}
	})
}

export const $loginByPhone = function(e) {
	return new Promise((resolve, reject) => {
		uni.login({
			success(res) {
				uni.setStorageSync('code', res.code)
				let params = {
					code: res.code,
					encryptedData: encodeURIComponent(e.detail.encryptedData),
					iv: encodeURIComponent(e.detail.iv)
				}
				uni.$u.http.post('/user/wx_login', params).then(({
					data,
					msg
				}) => {
					const userinfo = data.userinfo
					useDataStore().setUserInfo(userinfo)
					resolve(data)
				})

			}
		})
	})
}

export const $getSettingCallback = async ({
	auth_text, //权限文字 必填
	tips, //权限文字 必填
}) => {
	return new Promise((resolve, reject) => {
		wx.getSetting({
			success(res) {
				console.log('权限:::', res.authSetting);
				if (!res.authSetting[auth_text]) {
					wx.authorize({
						scope: auth_text,
						success: resolve,
						fail: (err) => {
							wx.showModal({
								title: '提示',
								content: tips,
								success: (modalSuccess) => {
									if (modalSuccess.confirm) {
										wx.openSetting({
											success: (data) => {
												if(data.authSetting[auth_text]){
													resolve()
												}else{
													reject()
												}
											},
											fail() {
												reject()
											}
										});
									}else{
										reject()
									}
								}
							});
						}
					})
				} else {
					resolve()
				}
			}
		})
	});

}